import { createApp } from 'vue'
import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
import ElementPlus from 'element-plus'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import 'element-plus/dist/index.css'
import App from './App.vue'
import router from './router'


// token 相关逻辑
function isTokenExpired(token: string | null) {
  if (!token) return true;
  const payload = JSON.parse(atob(token.split('.')[1]));
  const exp = payload.exp;
  const now = Math.floor(Date.now() / 1000);
  return exp < now;
}

const token = localStorage.getItem('token');
const isLoginPage = window.location.pathname === '/';

if (isTokenExpired(token) && !isLoginPage) {
  localStorage.removeItem('token');
  window.location.href = '/';
}

// vue app 初始化
const app = createApp(App)
const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)
app.use(pinia)
app.use(ElementPlus, { locale: zhCn })
app.use(router)
app.mount('#app')
